package org.file5.controller.admin;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.file5.domain.Person;
import org.file5.service.ServiceFascade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;

@Controller
@RequestMapping("/admin")
public class AdminController {
	private final Logger logger = Logger.getLogger(this.getClass());

	@Autowired
	private ServiceFascade sf;
	@Autowired
	private UserService googleUserService;

	@RequestMapping("/home")
	public String home(Model model, HttpServletRequest request) {
		User googleUser = googleUserService.getCurrentUser();
		String theSiteUid = request.getServerName()
				+ (request.getServerPort() == 80 ? "" : ":" + request.getServerPort() + "");

		Person person = sf.getPersonService().addUpdate(googleUser, theSiteUid,null);
		model.addAttribute("person", person);
		return "admin/home";
	}
	

	@RequestMapping("/blog/set")
	public String setAdminBlogInfo(@RequestParam(value = "blogJid") String blogJid, Model model,HttpServletRequest request) {

		User googleUser = googleUserService.getCurrentUser();
		String theSiteUid = request.getServerName()
		+ (request.getServerPort() == 80 ? "" : ":" + request.getServerPort() + "");

			sf.getPersonService().addUpdate(googleUser, theSiteUid,blogJid);
			return "redirect:/admin/home.htm";
		 
	}

}
